/*
 *  Copyright (C) 2010 Glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components.managers;

import java.util.Properties;
import javax.swing.JComboBox;
import org.balisunrise.application.Application;
import org.balisunrise.application.BaliProperties;
import org.balisunrise.common.BindingGroup;
import org.balisunrise.components.Converter;
import org.balisunrise.components.ComponentConverterException;
import org.balisunrise.components.ComponentManager;
import org.balisunrise.components.conveters.BooleanConverter;

/**
 *
 * @author glacuo
 */
public class BooleanComponentManager implements ComponentManager<Boolean, JComboBox>{

    private static String TRUE = BaliProperties.getString(BaliProperties.messageTRUE);
    private static String FALSE = BaliProperties.getString(BaliProperties.messageFALSE);
    private static final BooleanConverter CONVERTER = new BooleanConverter();

    public Class<Boolean> getType() {
        return Boolean.class;
    }

    public Class<JComboBox> getComponentType() {
        return JComboBox.class;
    }

    public JComboBox buildComponent(String componentName, Properties p) {

        JComboBox aux = new JComboBox();
        aux.setModel(new javax.swing.DefaultComboBoxModel(
                new String[] { TRUE, FALSE }));
        aux.setSelectedIndex(0);

        aux.setName(componentName);
        aux.getAccessibleContext().setAccessibleName(componentName);
        
        return aux;
    }

    public void clearComponentValue(JComboBox component) {
        if (null == component)
            return;
        if (component instanceof JComboBox){
            ((JComboBox)component).setSelectedIndex(0);
        }
    }

    public Object readValue(JComboBox component) {
        
        if (null == component)
            return null;

        if (component instanceof JComboBox){
            return ((JComboBox)component).getSelectedItem();
        }

        return null;
    }

    public Boolean readConvertedValue(JComboBox component,
            boolean emptyForAll) throws ComponentConverterException {

        Object obj = readValue(component);

        if (obj == null)
            return null;

        String value = (String)obj;

        try{
            return CONVERTER.convertReverse(value);
        }catch(Exception e){
            throw new ComponentConverterException(e.getMessage());
        }
    }

    public void insertValue(Boolean propertyValue, JComboBox component)
            throws ComponentConverterException {

        insertConvertedValue(CONVERTER.convertForward(propertyValue),component);
    }

    public void insertConvertedValue(Object propertyValue,
            JComboBox component) {

        if(component == null)
            return ;

        component.setSelectedItem(propertyValue);
    }

    public Converter<Boolean, ?> getConverter() {
        return CONVERTER;
    }

    public void createBinding(BindingGroup bindingGroup, Object source, String property, JComboBox component) {
        
        Application.get().getBindingBuilder().createBinding(
                bindingGroup,
                source,
                property,
                component,
                "selectedItem", // propriedade do componente a fazer o bind
                this,
                true,
                false);
    }
}
